$(function(){

    $("#note-json").pageJson({
        url : "ajax/note-json.php",
        type : "post",
        admin: 0,
        errorMessage: "Có lỗi xảy ra, không thể lấy được dữ liệu",
        contentFormat: 'div',
        onSuccess : function() {
            $.formDecorate();
        }
    });

    $("#note-json-admin").pageJson({
        url : "ajax/note-json.php",
        type : "post",
        admin: 1,
        errorMessage: "Có lỗi xảy ra, không thể lấy được dữ liệu",
        contentFormat: 'div',
        onSuccess : function() {
            $.formDecorate();
        },
        formFunctions : [
        {
            selector : '.edit-note',
            func : function(s) {
                return $.submitForm(s, function(form, data) {
                    if (data.code == 1) {
                        $("#note-json-admin").pageJsonRefresh(false);
                        $(form).find('.ajax-message').htmlTimer(data.message, 5000);
                    }
                    else {
                        $(form).find('.ajax-message').htmlTimer(data.message, 5000);
                    }
                });
            }
        },
        {
            selector : '.delete-note',
            func : function(s) {
                var data = {
                    action : 'delete_note'
                };
                var send = $.param(data);
                var message = "";
                $.ajax({
                    url : 'ajax/message-ajax.php',
                    data : send,
                    dataType : 'html',
                    type : 'POST',
                    success: function(data) {
                        message = data;
                        var c = confirm(message);
                        if (c) {
                            var rel = $(s).attr("title");
                            var val = $('#' + rel).find('input[name=create_date]').attr('value');
                            var data2 = {
                                action : 'delete_note',
                                create_date : val
                            };
                            var send = $.param(data2);

                            $('#' +rel).find('.ajax-load').show();
                            $(".form-button").addClass("disabled");
                            $(".form-button").attr("disabled","disabled");

                            $.ajax({
                                url : 'ajax/note-ajax.php',
                                type : 'POST',
                                dataType : 'json',
                                data: send,
                                success: function(data) {
                                    $('#' +rel).find('.ajax-load').hide();
                                    $(".form-button").removeAttr("disabled");
                                    $(".form-button").removeClass("disabled");
                                    $('#' + rel).find('.ajax-message').htmlTimer(data.message, 5000);
                                    if (data.code == 1) {
                                        $("#note-json-admin").pageJsonRefresh();
                                    }
                                    else {
                                        $('#' + rel).find(".ajax-message").htmlTimer(data.message, 5000);
                                    }
                                },
                                error: function() {
                                    $('#' + rel).find('.ajax-message').htmlTimer('Error', 5000);
                                    $('#' +rel).find('.ajax-load').hide();
                                    $(".form-button").removeAttr("disabled");
                                    $(".form-button").removeClass("disabled");
                                }
                            });
                        }
                    },
                    error: function() {
                        $(".ajax-message").htmlTimer("Error", 5000);
                    }
                });
                    
                return false;
            }
        }
        ]
    });

    $('.note-add-ajax-submit').click(function()
    {
        return $.submitForm(this, function (form, data) {
            if (data.code == 1) {
                $("#note-json-admin").pageJsonRefresh('first');
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
                $(form).clearForm();
            }
            else {
                $(form).find('.ajax-message').htmlTimer(data.message, 5000);
            }
        });
    });

});